package com.woniu.staff.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.util.ResponseData;
import com.woniu.staff.entity.Staff;
import com.woniu.staff.mapper.StaffMapper;
import com.woniu.staff.service.StaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author Administrator
* @description 针对表【t_staff(客服)】的数据库操作Service实现
* @createDate 2024-05-20 14:33:49
*/
@Service
@RequiredArgsConstructor
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff>
    implements StaffService{
    private final StringRedisTemplate redisTemplate;

    @Override
    public ResponseData<?> login(String account, String pwd) {
        String md5Pwd = SecureUtil.md5(pwd);
        Staff staff = lambdaQuery().eq(Staff::getAccount, account)
                .eq(Staff::getPwd, md5Pwd).one();
        if (ObjectUtil.isEmpty(staff)){
            return new ResponseData<>().fail("账户或密码错误");
        }

        Map<String, Object> userInfo = new HashMap<>(1);
        staff.setPwd(null);
        staff.setPhone(null);
        userInfo.put("user", staff);

        String jwt = JWTUtil.createToken(userInfo, "woniu".getBytes());
        redisTemplate.opsForValue().set(jwt, jwt, 30, TimeUnit.MINUTES);
        return new ResponseData<>().ok(jwt);
    }
}




